package com.ljx.offer;

/**
 * @author 李捷禧
 * Date: 2023/3/14
 * ClassName: C_29
 */

public class C_29 {
    public int[] spiralOrder(int[][] matrix) {
        //设置上下左右的边，进行遍历完，在边上做加减
        if(matrix.length == 0) return new int[0];
        //t是上，r是右，h是下，l是左；
        int t = 0, r = matrix[0].length - 1, l = 0, h = matrix.length - 1 , x = 0;
        //整合成一个数组
        int[] res = new int[(r+1)*(h+1)];
        while(true){
            for (int i = l; i <= r; i++) res[x++] = matrix[t][i];
            if(++t > h) break;
            for (int i = t; i <= h; i++) res[x++] = matrix[i][r];
            if(--r < l) break;
            for (int i = r; i >= l; i--) res[x++] = matrix[h][i];
            if(--h < t) break;
            for (int i = h; i >= t; i--) res[x++] = matrix[i][l];
            if (++l > r) break;
            }
        return res;
        }

    }

